﻿using DatabaseUserTest.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseUserTest.Controller
{
    public class PlaneCTRL
    {
        DBPlane dbPlane = new DBPlane();
        DBPlaneInterior dbInterior = new DBPlaneInterior();
        DBRoute dbRoute = new DBRoute();
        DBDestination dbDest = new DBDestination();

        public int FindAvailableNormalSeats(int planeId)
        {
            DBPlane dbPlane = new DBPlane();
            Plane plane = dbPlane.FindPlane(planeId);
            return plane.PlaneInterior.TotalNormalSeats - plane.ReservedNormalSeats;
        }

        public Plane FindPlane(DateTime departure, int routeId)
        {
            return dbPlane.FindPlane(departure, routeId);
        }

        public int FindAvailableFirstClassSeats(int planeId)
        {
            DBPlane dbPlane = new DBPlane();
            Plane plane = dbPlane.FindPlane(planeId);
            return plane.PlaneInterior.TotalFirstClassSeats - plane.ReservedFirstClassSeats;
        }

        public List<Plane> FindAllPlanes()
        {
            return dbPlane.FindAllPlanes();
        }

        public void ReserveSeats(int check, List<Plane> planeList, int seats)
        {
            dbPlane.ReserveSeats(check, planeList, seats);
        }

        public void UnreserveSeats(int check, List<Plane> planeList, int seats)
        {
            dbPlane.UnreserveSeats(check, planeList, seats);
        }

        public bool AddPlane(string interiorId, string gate, DateTime departure, int routeId)
        {
            using (var db = new DatabaseContext())
            {

                Plane plane = new Plane();
                int gateresult;
                int interiorresult;

                if (int.TryParse(interiorId, out interiorresult) && dbInterior.FindPlaneInterior(interiorresult) != null && (int.TryParse(gate, out gateresult) && gateresult > 0) && dbRoute.FindRoute(routeId) != null)
                {
                    Route route = dbRoute.FindRoute(routeId);
                    PlaneInterior interior = dbInterior.FindPlaneInterior(interiorresult);
                    plane.Gate = gateresult;
                    plane.InteriorId = interiorresult;
                    plane.RouteId = routeId;
                    plane.Departure = departure;
                    plane.ReservedFirstClassSeats = 0;
                    plane.ReservedNormalSeats = 0;
                    plane.Arrival = ArrivalCalculator(plane);
                    dbPlane.AddPlane(plane);

                    if (dbPlane.FindPlane(plane.PlaneId) != null)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }

        public DateTime ArrivalCalculator(Plane plane)
        {
            DateTime arrivalTemp = plane.Departure;
            RouteCTRL routeCtrl = new RouteCTRL();
            Route route = routeCtrl.FindRoute(plane.RouteId);
            string time = Convert.ToString(route.Time);
            string[] timeSplit = time.Split(',');
            int hours = Convert.ToInt32(timeSplit[0]);

            arrivalTemp = arrivalTemp.AddHours(hours);

            if (time.Contains(",5"))
            {
                arrivalTemp = arrivalTemp.AddMinutes(30);
            }

            return arrivalTemp;
        }
    }
}
